<template>
  <div class="app-container">
    <el-row :gutter="20" style="margin-bottom: 8px;height: 20px">
      <el-col :span="4">
        <el-form-item label="处方类型" prop="prescriptionType">
          <el-radio-group v-model="prescriptionType"
                          class="prescriptionType-radio"
                          @change="prescriptionTypeChange">
            <el-radio v-for="(dict, index) in dictDataMap['drug_classification']"
                      v-show="dict.dictLabel === '西药' || dict.dictLabel === '中草药'"
                      :key="dict.dictValue"
                      :value="dict.dictValue">{{
                dict.dictLabel
              }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-col>
      <el-col :span="16">
        <el-form-item>
          <el-button type="primary" @click="getList">
            <template #icon>
              <i-ep-search/>
            </template>
            刷新
          </el-button>
          <el-button v-if="prescriptionType ==='1'" plain type="primary" @click="handleAddDrug">
            <template #icon>
              <i-ep-plus/>
            </template>
            添加药品
          </el-button>
          <el-button v-if="prescriptionType ==='1'" plain type="success" @click="submitForm">
            <svg-icon class="mr5" icon-class="save"/>
            模板保存
          </el-button>
        </el-form-item>
      </el-col>
    </el-row>

    <div style="border: 1px solid #027687;height: calc(100% - 20px)">
      <western-prescription-template v-if="prescriptionType ==='1'" ref="westernRef"
                                     :org-id="orgId" :template-master-id="props.templateMasterId" @sub-ok="saveOk"/>
      <chinese-prescription-template v-if="prescriptionType ==='2'" ref="chineseRef"
                                     :org-id="orgId" :template-master-id="props.templateMasterId"/>
    </div>
  </div>
</template>

<script lang="ts" setup>
import ChinesePrescriptionTemplate from "./chinesePrescriptionTemplate.vue"
import WesternPrescriptionTemplate from "./westernPrescriptionTemplate.vue"

import {useDictStoreHook} from "@/store/modules/dict.ts";
import {insertBoByWholeSet} from "@/api/patient/templateDetail.ts";

const dictStore = useDictStoreHook();
let dictDataMap = ref(dictStore.isTypeGetData('prescription_type', 'drug_classification', 'bill_item_class_dict',
  'charge_flag_dict', 'grant_flag', 'sys_user_sex', 'diagnosis_type', 'chinese_western_type'))
const {proxy} = getCurrentInstance() as any;
const props = defineProps({
  templateMasterId: {
    type: String,
    default: ""
  },
  orgId: {
    type: String,
    default: ""
  },
  deptId: {
    type: String,
    default: ""
  },
})

const westernRef = ref()
const chineseRef = ref()
const storageId = ref()
const drugVsDeptList = ref<any>([])
const prescriptionType = ref<String>("1")
const drugClass = ref<String>("")
const prescAttr = ref<String>("")

function prescriptionTypeChange() {
  getList()
  if (prescriptionType.value === "1") {
    // 西药
    drugClass.value = "1,3"
    prescAttr.value = "1"
    nextTick(() => {
      westernRef.value.setPrescInfo(drugClass.value, prescAttr.value)
    })
  } else {
    // 中药
    drugClass.value = "2"
    prescAttr.value = "2"
    nextTick(() => {
      chineseRef.value.setPrescInfo(drugClass.value, prescAttr.value)
    })
  }
}

/**
 * 添加模板
 */
function handleAddDrug() {
  if (prescriptionType.value === '1') {
    nextTick(() => {
      westernRef.value.handleAddDrugOk()
    })
  }
  // else {
  //   nextTick(() => {
  //     chineseRef.value.handleAddDrugOk()
  //   })
  // }
}


function initInfo() {
  prescriptionType.value = "1"
  prescriptionTypeChange()
}

function getList() {
  if (prescriptionType.value === "1") {
    nextTick(() => {
      westernRef.value.getList()
    })
  } else {
    nextTick(() => {
      chineseRef.value.getList()
    })
  }
}

/**
 * 保存药品
 */
function submitForm() {
  if (prescriptionType.value === "1") {
    nextTick(() => {
      westernRef.value.getFormTable()
    })
  }
}

/**
 * 最终保存
 * @param tableList
 */
function saveOk(tableList: any) {
  ElMessageBox.confirm('确定要进行保存操作吗？',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    tableList.forEach((item: any) => {
      item.noSkin = item.noSkin ? 'Y' : 'N'
      item.isSkin = item.noSkin ? 'N' : 'Y'
      item.itemClass = "A"
    })
    const data = {
      "detailBoList": tableList,
      "orgId": props.orgId,
      "type": "1",
      "itemClass": "A",
      "templateMasterId": props.templateMasterId,
    }
    insertBoByWholeSet(data).then(() => {
      ElMessage.success("药品明细保存成功")
      if (prescriptionType.value === "1") {
        nextTick(() => {
          westernRef.value.getList()
        })
      }
    })
  })
}

defineExpose({
  initInfo
})
</script>

<style scoped>
:deep(.el-input-group__append) {
  padding: 0 5px;
  width: 40px;
}

:deep(.el-input-group__prepend) {
  padding-left: 5px;
  padding-right: 5px;
}

</style>
